package com.morningstar.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysDictData;
import com.morningstar.system.dao.mapper.SysDictDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Slf4j
@Service
public class SysDictDataService extends ServiceImpl<SysDictDataMapper, SysDictData> {

    public ServiceCheckResult checkAddAndUpdate(SysDictData sysDictData) {
        LambdaQueryWrapper<SysDictData> checkDictLabelWrapper = new LambdaQueryWrapper<SysDictData>()
                .eq(SysDictData::getDictType, sysDictData.getDictType())
                .eq(SysDictData::getDictLabel, sysDictData.getDictLabel())
                .ne(sysDictData.getDictCode() != null, SysDictData::getDictCode, sysDictData.getDictCode());
        if (this.count(checkDictLabelWrapper) > 0) {
            return new ServiceCheckResult(true, "字典标签已存在");
        }
        LambdaQueryWrapper<SysDictData> checkDictValueWrapper = new LambdaQueryWrapper<SysDictData>()
                .eq(SysDictData::getDictType, sysDictData.getDictType())
                .eq(SysDictData::getDictValue, sysDictData.getDictValue())
                .ne(sysDictData.getDictCode() != null, SysDictData::getDictCode, sysDictData.getDictCode());
        if (this.count(checkDictValueWrapper) > 0) {
            return new ServiceCheckResult(true, "字典数据值已存在");
        }
        return new ServiceCheckResult(false);
    }
}
